1615C - Menorah - CodeForces Solution


brute force graphs greedy math *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define FILEIO 0

#if FILEIO == 1
    #define IN input
    #define OUT output
#else
    #define IN cin
    #define OUT cout
#endif

#define FOR(var, start, end) for(int var = start; var < end; var++)
#define ull int64_t


constexpr long double pi = 3.141592653589793238462643383279;
     

int T, N;
string A, B;

int64_t solve()
{
    int a, b, c, d;
    a = b = c = d = 0;
    for(int i=0; i < N; i++)
    {
        if(A[i] == '1' && B[i] == '1')
            a++;
        if(A[i] == '1' && B[i] == '0')
            b++;
        if(A[i] == '0' && B[i] == '1')
            c++;
        if(A[i] == '0' && B[i] == '0')
            d++;
    }

    int AB = INT_MAX;
    int BA = INT_MAX;

    if(b == c && b == 0)
        return 0;

    if(b == c)
        BA = 2*b;

    swap(a,c);
    swap(b,d);
    a++;
    c--;

    if(b == c)
        AB = 2 * b + 1;

    /*
    if(a == 1 + d)
    {
        int count = 0;
        int par = 0;
        while(true)
        {
            if(b == 0 && c == 0)
                break;
            if(!par)
            {
                swap(a,c);
                swap(b,d);
                a++;
                c--;
            } else
            {
                swap(a,c);
                swap(b,d);
                b++;
                d--;
            }
            par = !par;
            count++;
        }
        AB = count;
    }
    */


    if(AB == BA && AB == INT_MAX)
        return -1;

    return min(AB, BA);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

#if FILEIO == 1
    ifstream input("input.txt");
    ofstream output("output.txt");
#endif

    //////////////////////////////////////////////////////
    //////////////////////////////////////////////////////
    
    IN >> T;
    FOR(t,0,T)
    {
        IN >> N;
        IN >> A >> B;
        auto res = solve();
        OUT << res << "\n";
    }
    OUT << endl;
}


Comments

Submit
0 Comments
More Questions

620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll